package com.x52im.rainbowchat.http.logic.dto;


import com.x52im.rainbowchat.http.util.JedisTools;
import org.apache.commons.lang.RandomStringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 红包实体
 */
public class RedEnvelopeDTO implements Serializable {
    public static String ON_CONF = "RE_ORDER_NO";

    protected String id;    // 红包ID
    protected String parentId = "-1";  // 子红包才有值
    protected String title; // 红包标题(如恭喜发财)
    protected String orderNo;   // 红包编号
    protected String userUid;
    protected String totalAmount;
    protected String createDate;
    protected String stateDate;
    protected String state = STATE.STATE_INIT.getState();
    protected String memo;
    protected String subNumber = "1";    // 子红包个数
    protected String subMoney;    // 子红包金额,为空则表示随机,不为空则表示固定金额
    protected String gId;   // 红包发在哪个群

    @Deprecated
    protected String pId;   // 红包私发给谁

    protected String getNum = "0";  // 已领取的数量
    protected String getMoney = "0";  // 已领取的金额

    protected String nickname;
    protected String avatar;

    protected String isMax = "0"; // 是否为最高金额的红包,1是

    protected String needAlipayList;    // 是否需要账户的充值和提现明细,1需要

    public static String getRedisKey(String id) {
        String lockKey = String.format("%s%s", JedisTools.PRE_RED_ENVELOPE_KEY, id);
        return lockKey;
    }

    public static String getRedisKeyForLock(String id) {
        String lockKey = String.format("%s%s%s", JedisTools.PRE_RED_ENVELOPE_KEY, id, "-lock");
        return lockKey;
    }

    public static int REDIS_DB_INDEX = 1;  // 红包缓存放在哪个redis库

    public static String getOrderNo(String uid) {
        String prefix = "REDENV_" + uid + "_";
        String nano = String.valueOf(System.nanoTime());
        String rnd = RandomStringUtils.randomAlphanumeric(3);
        String no = String.format("%s%s%s", prefix, nano, rnd);
        return no;
    }

    public int getTotalAmountInt() {
        if (totalAmount == null) {
            return 0;
        }
        return Integer.parseInt(totalAmount);
    }

    public int getSubNumberInt() {
        if (subNumber == null) {
            return 0;
        }
        return Integer.parseInt(subNumber);
    }

    public String getNeedAlipayList() {
        return needAlipayList;
    }

    public void setNeedAlipayList(String needAlipayList) {
        this.needAlipayList = needAlipayList;
    }

    public List<RedEnvelopeDTO> getSubRedEnvelopeDTOS() {
        return subRedEnvelopeDTOS;
    }

    public void setSubRedEnvelopeDTOS(List<RedEnvelopeDTO> subRedEnvelopeDTOS) {
        this.subRedEnvelopeDTOS = subRedEnvelopeDTOS;
    }

    List<RedEnvelopeDTO> subRedEnvelopeDTOS = new ArrayList<>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public String getUserUid() {
        return userUid;
    }

    public void setUserUid(String userUid) {
        this.userUid = userUid;
    }

    public String getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(String totalAmount) {
        this.totalAmount = totalAmount;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCreateDate() {
        return createDate;
    }

    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }

    public String getStateDate() {
        return stateDate;
    }

    public void setStateDate(String stateDate) {
        this.stateDate = stateDate;
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public String getSubNumber() {
        return subNumber;
    }

    public void setSubNumber(String subNumber) {
        this.subNumber = subNumber;
    }

    public String getSubMoney() {
        return subMoney;
    }

    public void setSubMoney(String subMoney) {
        this.subMoney = subMoney;
    }

    public String getGetNum() {
        return getNum;
    }

    public void setGetNum(String getNum) {
        this.getNum = getNum;
    }

    public String getGetMoney() {
        return getMoney;
    }

    public void setGetMoney(String getMoney) {
        this.getMoney = getMoney;
    }

    public String getIsMax() {
        return isMax;
    }

    public void setIsMax(String isMax) {
        this.isMax = isMax;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickName(String nickname) {
        this.nickname = nickname;
    }

    public String getgId() {
        return gId;
    }

    public void setgId(String gId) {
        this.gId = gId;
    }

    public String getpId() {
        return pId;
    }

    public void setpId(String pId) {
        this.pId = pId;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public void setUNKNOWED(String memo) {
        this.state = STATE.STATE_UNKNOWED_ERR.getState();
        this.memo = memo == null ? STATE.STATE_UNKNOWED_ERR.getStateMemo() : memo;
    }

    public void setFINISHAll() {
        this.state = STATE.STATE_FINISH_ALL.getState();
        this.memo = STATE.STATE_FINISH_ALL.getStateMemo();
    }

    public void setFINISH() {
        this.state = STATE.STATE_FINISH.getState();
        this.memo = STATE.STATE_FINISH.getStateMemo();
    }

    public void setSUCCESSSend() {
        this.state = STATE.STATE_SEND_SUCCESS.getState();
        this.memo = STATE.STATE_SEND_SUCCESS.getStateMemo();
    }

    public void setErrNotFound() {
        this.state = STATE.STATE_NOT_FOUND_ERR.getState();
        this.memo = STATE.STATE_NOT_FOUND_ERR.getStateMemo();
    }

    public void setErrNotEnough() {
        this.state = STATE.STATE_SEND_NOT_ENOUGH_MONEY.getState();
        this.memo = STATE.STATE_SEND_NOT_ENOUGH_MONEY.getStateMemo();
    }

    public void setErrDeduct(String memo) {
        this.state = STATE.STATE_SEND_DEDUCT_ERR.getState();
        this.memo = STATE.STATE_SEND_DEDUCT_ERR.getStateMemo();
    }

    public void setErrIncrease() {
        this.state = STATE.STATE_SEND_INCREASE_ERR.getState();
        this.memo = STATE.STATE_SEND_INCREASE_ERR.getStateMemo();
    }

    public enum STATE {
        STATE_INIT("0", "初始化"),
        STATE_SEND_SUCCESS("1", "红包发送成功"),
        STATE_SEND_NOT_ENOUGH_MONEY("-1", "账户余额不足,不能发红包"),
        STATE_SEND_DEDUCT_ERR("-2", "发红包时账户扣减失败"),
        STATE_SEND_INCREASE_ERR("-3", "收红包时账户增加余额失败"),
        STATE_UNKNOWED_ERR("-10", "未知错误"),
        STATE_NOT_FOUND_ERR("-11", "红包找不到了"),
        STATE_FINISH_ALL("8", "全部红包领取完成"),
        STATE_FINISH("9", "当前红包领取完成");
        private String state;
        private String stateMemo;

        STATE(String state, String stateMemo) {
            this.state = state;
            this.stateMemo = stateMemo;
        }

        public String getState() {
            return state;
        }

        public void setState(String state) {
            this.state = state;
        }

        public String getStateMemo() {
            return stateMemo;
        }

        public void setStateMemo(String stateMemo) {
            this.stateMemo = stateMemo;
        }
    }

    public Map toMap() {
        Map map = new HashMap();
        map.put("id", this.id);
        map.put("title", this.title);
        map.put("parentId", this.parentId);
        map.put("orderNo", this.orderNo);
        map.put("userUid", this.title);
        map.put("totalAmount", this.totalAmount);
        map.put("state", this.state);
        map.put("createDate", this.createDate);
        map.put("stateDate", this.stateDate);
        map.put("memo", this.memo);
        map.put("subNumber", this.subNumber);
        map.put("subMoney", this.subMoney);
        map.put("subRedEnvelopes", this.subRedEnvelopeDTOS);
        return map;
    }
}
